<template>
	<view>
		<uni-forms ref="form" :modelValue="formData" :rules="rules">
			<uni-forms-item label="姓名" name="name">
				<uni-easyinput type="text" v-model="formData.name" placeholder="请输入姓名" />
			</uni-forms-item>
		</uni-forms>
		<button @click="submit">Submit</button>
		<view class="">
			{{formData.name}}
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				formData: {
					name: '111',
					hobby: '222',
				},
				rules: {
					// 对name字段进行必填验证
					name: {
						rules: [{
								required: true,
								errorMessage: '请输入姓名',
							},
							{
								minLength: 3,
								maxLength: 5,
								errorMessage: '姓名长度在 {minLength} 到 {maxLength} 个字符',
							}
						]
					},
					// 对email字段进行必填验证
					email: {
						rules: [{
							format: 'email',
							errorMessage: '请输入正确的邮箱地址',
						}]
					}
				},
			}
		},
		methods: {
			submit() {
				this.$refs.form.validate().then(res=>{
					console.log('表单数据信息：', res);
					uni.request({
					    url: 'http://localhost:8080/user/queryUserList.do', //仅为示例，并非真实接口地址。
					    data: {
					        name: this.formData.name,
					    },
					    header: {
					        'custom-header': 'hello' //自定义请求头信息
					    },
					    success: (res) => {
					        console.log(res.data);
					    }
					});
				}).catch(err =>{
					console.log('表单错误信息：', err);
				})
			}
		}
	}
</script>

<style>

</style>
